登录 白背景

24. 两两交换链表中的节点

https://leetcode.cn/problems/swap-nodes-in-pairs/

  • 提交时间:2022-05-25 08:57:05
  • 执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
  • 内存消耗:1.9 MB, 在所有 Go 提交中击败了58.54%的用户
  • 通过测试用例:55 / 55
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func swapPairs(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }
    tmp := head.Next
    head.Next = swapPairs(head.Next.Next)
    tmp.Next = head
    return tmp
}